package cn.mily.org.service.impl;

import cn.mily.basics.service.impl.BasicServiceImpl;
import cn.mily.org.domain.Product;
import cn.mily.org.domain.ProductDetail;
import cn.mily.org.mapper.ProductDetailMapper;
import cn.mily.org.mapper.ProductMapper;
import cn.mily.org.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper mapper;

    @Autowired
    private ProductDetailMapper detailMapper;

    /**
     * 更新
     * @param product
     */
    @Override
    public void update(Product product) {
        /*更新*/
        super.update(product);
        ProductDetail detail = product.getProductDetail();
        detail.setProduct_id(product.getId());
        detailMapper.update(detail);
    }

    /**
     * 添加
     * @param product
     */
    @Override
    public void add(Product product) {
        /*先添加产品 返回主键id*/
        super.add(product);
        /*获取详情*/
        ProductDetail productDetail = product.getProductDetail();
        /*设置产品id*/
        productDetail.setProduct_id(product.getId());
        /*详情添加*/
        detailMapper.add(productDetail);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void patchDelete(Long[] ids) {
        /*先删除详情信息*/
        detailMapper.patchDelete(ids);
        super.patchDelete(ids);
    }

    /**
     * 单个删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        /*先删除详情信息*/
        detailMapper.delete(id);
        /*再删除自身*/
        super.delete(id);
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    public void patchOn(Long[] ids) {
        mapper.patchOn(ids);
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    public void patchOff(Long[] ids) {
        mapper.patchOff(ids);
    }
}
